package com.hm.ibsm.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.ServerResponse;
import com.hm.ibsm.manage.enums.DictTypeEnums;
import com.hm.ibsm.manage.service.DictService;
import com.hm.ibsm.manage.vo.dict.DictTypeValueVo;
import com.hm.ibsm.manage.vo.dict.DictVo;
import com.hm.ibsm.util.ControllerResult;
import com.hm.ibsm.util.R.API;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2017年3月7日 上午11:42:25
 * @version 1.0
 * @describe
 */
@Slf4j
@ResponseBody
@RestController
@RequestMapping(API.MODULE_NAME + "/dict")
public class ApiDict {

	private static final String MODULE_TAG_NAME = "字典管理";

	@Autowired
	private DictService dictService;

	@RequestMapping(path = "/{pageNumber}/{pageSize}", method = RequestMethod.GET)
	@ApiOperation(tags = MODULE_TAG_NAME, value = "分页查询字典信息", httpMethod = "GET", notes = "分页查询字典信息")
	public ResponseEntity<ServerResponse<PageInfo<DictVo>>> query(
			@ApiParam(required = true, name = "pageNumber", value = "分页号(第一页值为1,最小页码不能小于1)") @PathVariable("pageNumber") Integer pageNumber,
			@ApiParam(required = true, name = "pageSize", value = "分页大小") @PathVariable("pageSize") Integer pageSize,
			@ApiParam(required = false, name = "dictType", value = "字典类型") @RequestParam(name = "dictType", required = false) DictTypeEnums dictType) {
		try {
			return ControllerResult.success(dictService.query(new PageInfo.PageParam(pageNumber, pageSize), dictType));
		} catch (Exception e) {
			log.error(MODULE_TAG_NAME, e);
			return ControllerResult.failed(e);
		}
	}

	@RequestMapping(path = "/{dictType}", method = RequestMethod.GET)
	@ApiOperation(tags = MODULE_TAG_NAME, value = "获取指定字典类型字典数据集合", httpMethod = "GET", notes = "获取指定字典类型字典数据集合")
	public ResponseEntity<ServerResponse<List<DictTypeValueVo>>> getDictTypeValues(
			@ApiParam(required = true, name = "dictType", value = "字典类型") @PathVariable(name = "dictType", required = true) DictTypeEnums dictType) {
		try {
			return ControllerResult.success(dictService.getDictTypeValues(dictType));
		} catch (Exception e) {
			log.error(MODULE_TAG_NAME, e);
			return ControllerResult.failed(e);
		}
	}

	@RequestMapping(path = "/value/{code}", method = RequestMethod.GET)
	@ApiOperation(tags = MODULE_TAG_NAME, value = "获取指定字典编码数据", httpMethod = "GET", notes = "获取指定字典编码数据")
	public ResponseEntity<ServerResponse<DictTypeValueVo>> getDictValue(
			@ApiParam(required = true, name = "code", value = "字典编码") @PathVariable(name = "code", required = true) String code) {
		try {
			return ControllerResult.success(dictService.getDictValue(code));
		} catch (Exception e) {
			log.error(MODULE_TAG_NAME, e);
			return ControllerResult.failed(e);
		}
	}

	@RequestMapping(path = "/values/{code}", method = RequestMethod.GET)
	@ApiOperation(tags = MODULE_TAG_NAME, value = "获取指定字典编码数据集合", httpMethod = "GET", notes = "获取指定字典编码数据集合")
	public ResponseEntity<ServerResponse<List<DictTypeValueVo>>> getDictValues(
			@ApiParam(required = true, name = "code", value = "字典编码") @PathVariable(name = "code", required = true) String code) {
		try {
			return ControllerResult.success(dictService.getDictValues(code));
		} catch (Exception e) {
			log.error(MODULE_TAG_NAME, e);
			return ControllerResult.failed(e);
		}
	}
}
